
package com.joysuch.wwyt.core.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.joysuch.wwyt.core.entity.BaseLoginAccount;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.enums.LoginUserStates;
import com.joysuch.wwyt.core.repository.BaseLoginAccountDao;
import com.joysuch.wwyt.core.service.BaseLoginAccountService;
import com.joysuch.wwyt.core.test.TestBean;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/validate")
public class ValidationController {

    @Autowired
    private BaseLoginAccountDao userDao;

    @Autowired
    private BaseLoginAccountService loginAccountService;

    @RequiresPermissions("test:validate")
    @ResponseBody
    @RequestMapping("test")
    public Object validate(@Valid TestBean bean, HttpServletRequest request, BindingResult result) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("data", bean);
        return map;
    }

    @RequestMapping("test2")
    public Object validate2(TestBean bean, HttpServletRequest request, BindingResult result) {
        bean.getName().charAt(100);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("data", bean);
        return map;
    }

    @ResponseBody
    @RequestMapping("addLoginUser")
    public Object addLoginUser(BaseLoginAccount bean, HttpServletRequest request, BindingResult result)
            throws Exception {
        bean.setDeleteFlag(DeleteFlag.EFFECTIVE.getCode());
        bean.setState(LoginUserStates.NORMAL.getCode());
        loginAccountService.save(bean);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("resultCode", 0);
        map.put("data", "添加成功");
        return map;
    }

}
